#include <stdlib.h>
#include <stdio.h>
#include <string.h>


int main() {

  /* Goal: Given a c string that holds a hexidecimal
   * digit - parse the string, extract the number
   * portion and convert it into an `int'.
   *
   * 	0xNN   --> can be stored in an 8 bit int.
   * 	0xNNNN --> can be stored in an 16 bit int. 
   *
   * The, we will call the c library's sscanf() 
   * function to take care of the string parsing
   * and conversion. */

  /* c strings need a terminating null to work 
   * properly/reliably with c stdio.  So, we 
   * allocat a string with 1 extra character - 
   * and initialize all characters to 0. */
  char IntStr8bit[4+1] = { 0 };
  char IntStr16bit[8+1] = { 0 };

  /* on most modern pc's - an int is either 32 or
   * 64 bits.  So, we can safely use a regular `int'
   * to store the results. The variable names do not
   * denote the size of the datatype `int' - only the
   * maximum hex number the string buffer can hold.
   *
   * Also, these are unsigned b/c our string buffer
   * has no room for a sign. */
  unsigned int hex8bit_conv = 0; 
  unsigned int hex16bit_conv = 0;

  /* copy hex strings into their respective buffers -
   * see `man strncpy' for details */
  strncpy(IntStr8bit, "0xff", 4+1);
  strncpy(IntStr16bit, "0xffff", 8+1);

  /* go go gadget... convert to integers */
  sscanf(IntStr8bit, "0x%2x", &hex8bit_conv);
  sscanf(IntStr16bit, "0x%4x", &hex16bit_conv);

  /* print the results */
  printf("here is the 8bit hex string converted to an int: %d\n", hex8bit_conv);
  printf("here is the 16bit hex string converted to an int: %d\n", hex16bit_conv);

  return(0);
}
